//
//  RFAdmobProtocol.swift
//  RFAds
//
//  Created by Roffa Zhou on 2023/2/9.
//

import Foundation

public struct RFAdmobSplashNotif {
    static let loadedNotifName = Notification.Name("rf_admob_loaded_notif_key")  //开屏广告加载成功通知名
    static let loadErrNotifName = Notification.Name("rf_admob_loadErr_notif_key")//开屏广告加载失败通知名
    static let showedNotifName = Notification.Name("rf_admob_showed_notif_key")//开屏广告显示成功通知名
    static let dismissNotifName = Notification.Name("rf_admob_dismiss_notif_key")//开屏广告页已关闭通知名
    static let willDismissNotifName = Notification.Name("rf_admob_will_dismiss_notif_key")//开屏广告页将要关闭通知名

    static let showErrNotifName = Notification.Name("rf_admob_showErr_notif_key") //开屏广告显示失败通知名
}
public struct RFAdmobInterstitialNotif {
    static let loadedNotifName = Notification.Name("rf_interstitial_loaded_notif_key")  //插页广告加载成功通知名
    static let loadErrNotifName = Notification.Name("rf_interstitial_loadErr_notif_key")//插页广告加载失败通知名
    static let showedNotifName = Notification.Name("rf_interstitial_showed_notif_key")//插页广告显示成功通知名
    static let dismissNotifName = Notification.Name("rf_interstitial_dismiss_notif_key")//插页广告页已关闭通知名
    static let willDismissNotifName = Notification.Name("rf_interstitial_will_dismiss_notif_key")//插页广告页将要关闭通知名

    static let showErrNotifName = Notification.Name("rf_interstitial_showErr_notif_key") //插页广告显示失败通知名
}
public struct RFAdmobRewardNotif {
    static let loadedNotifName = Notification.Name("rf_reward_loaded_notif_key")  //激励广告加载成功通知名
    static let loadErrNotifName = Notification.Name("rf_reward_loadErr_notif_key")//激励广告加载失败通知名
    static let showedNotifName = Notification.Name("rf_reward_showed_notif_key")//激励广告显示成功通知名
    static let dismissNotifName = Notification.Name("rf_reward_dismiss_notif_key")//激励广告页已关闭通知名
    static let willDismissNotifName = Notification.Name("rf_reward_will_dismiss_notif_key")//激励广告页将要关闭通知名
    static let rewardNotifName = Notification.Name("rf_reward_receive_notif_key")//激励广告页已派发奖励

    static let showErrNotifName = Notification.Name("rf_reward_showErr_notif_key") //激励广告显示失败通知名
}

public struct RFAdmobUtils {
    public static func postSplashLoadedNotif(){
        NotificationCenter.default.post(name: RFAdmobSplashNotif.loadedNotifName, object: nil)
    }
    public static func addSplashLoadedNotif(_ callback:@escaping ()->Void){
        NotificationCenter.default.addObserver(forName: RFAdmobSplashNotif.loadedNotifName, object: nil, queue: .main) { notif in
            callback()
        }
    }
    
    public static func postSplashLoadeErrNotif(_ err: NSError?){
        NotificationCenter.default.post(name: RFAdmobSplashNotif.loadErrNotifName, object: err)
    }
    public static func addSplashLoadErrNotif(_ callback:@escaping (NSError?)->Void){
        NotificationCenter.default.addObserver(forName: RFAdmobSplashNotif.loadErrNotifName, object: nil, queue: .main) { notif in
            callback(notif.object as? NSError)
        }
    }
    
    public static func postSplashShowedNotif(){
        NotificationCenter.default.post(name: RFAdmobSplashNotif.showedNotifName, object: nil)
    }
    public static func addSplashShowedNotif(_ callback:@escaping ()->Void){
        NotificationCenter.default.addObserver(forName: RFAdmobSplashNotif.showedNotifName, object: nil, queue: .main) { notif in
            callback()
        }
    }
    
    public static func postSplashDismissNotif(){
        NotificationCenter.default.post(name: RFAdmobSplashNotif.dismissNotifName, object: nil)
    }
    public static func addSplashDismissNotif(_ callback:@escaping ()->Void){
        NotificationCenter.default.addObserver(forName: RFAdmobSplashNotif.dismissNotifName, object: nil, queue: .main) { notif in
            callback()
        }
    }
    public static func postSplashWillDismissNotif(){
        NotificationCenter.default.post(name: RFAdmobSplashNotif.willDismissNotifName, object: nil)
    }
    public static func addSplashWillDismissNotif(_ callback:@escaping ()->Void){
        NotificationCenter.default.addObserver(forName: RFAdmobSplashNotif.willDismissNotifName, object: nil, queue: .main) { notif in
            callback()
        }
    }
    
    public static func postSplashShowErrNotif(_ err: NSError?){
        NotificationCenter.default.post(name: RFAdmobSplashNotif.showErrNotifName, object: err)
    }
    public static func addSplashShowErrNotif(_ callback:@escaping (NSError?)->Void){
        NotificationCenter.default.addObserver(forName: RFAdmobSplashNotif.showErrNotifName, object: nil, queue: .main) { notif in
            callback(notif.object as? NSError)
        }
    }
}

extension RFAdmobUtils {
    public static func postInterstitialLoadedNotif(){
        NotificationCenter.default.post(name: RFAdmobInterstitialNotif.loadedNotifName, object: nil)
    }
    public static func addInterstitialLoadedNotif(_ callback:@escaping ()->Void){
        NotificationCenter.default.addObserver(forName: RFAdmobInterstitialNotif.loadedNotifName, object: nil, queue: .main) { notif in
            callback()
        }
    }
    
    public static func postInterstitialLoadeErrNotif(_ err: NSError?){
        NotificationCenter.default.post(name: RFAdmobInterstitialNotif.loadErrNotifName, object: err)
    }
    public static func addInterstitialLoadErrNotif(_ callback:@escaping (NSError?)->Void){
        NotificationCenter.default.addObserver(forName: RFAdmobInterstitialNotif.loadErrNotifName, object: nil, queue: .main) { notif in
            callback(notif.object as? NSError)
        }
    }
    
    public static func postInterstitialShowedNotif(){
        NotificationCenter.default.post(name: RFAdmobInterstitialNotif.showedNotifName, object: nil)
    }
    public static func addInterstitialShowedNotif(_ callback:@escaping ()->Void){
        NotificationCenter.default.addObserver(forName: RFAdmobInterstitialNotif.showedNotifName, object: nil, queue: .main) { notif in
            callback()
        }
    }
    
    public static func postInterstitialDismissNotif(){
        NotificationCenter.default.post(name: RFAdmobInterstitialNotif.dismissNotifName, object: nil)
    }
    public static func addInterstitialDismissNotif(_ callback:@escaping ()->Void){
        NotificationCenter.default.addObserver(forName: RFAdmobInterstitialNotif.dismissNotifName, object: nil, queue: .main) { notif in
            callback()
        }
    }
    public static func postInterstitialWillDismissNotif(){
        NotificationCenter.default.post(name: RFAdmobInterstitialNotif.willDismissNotifName, object: nil)
    }
    public static func addInterstitialWillDismissNotif(_ callback:@escaping ()->Void){
        NotificationCenter.default.addObserver(forName: RFAdmobInterstitialNotif.willDismissNotifName, object: nil, queue: .main) { notif in
            callback()
        }
    }
    
    public static func postInterstitialShowErrNotif(_ err: NSError?){
        NotificationCenter.default.post(name: RFAdmobInterstitialNotif.showErrNotifName, object: err)
    }
    public static func addInterstitialShowErrNotif(_ callback:@escaping (NSError?)->Void){
        NotificationCenter.default.addObserver(forName: RFAdmobInterstitialNotif.showErrNotifName, object: nil, queue: .main) { notif in
            callback(notif.object as? NSError)
        }
    }
}

extension RFAdmobUtils {
    public static func postRewardLoadedNotif(){
        NotificationCenter.default.post(name: RFAdmobRewardNotif.loadedNotifName, object: nil)
    }
    public static func addRewardLoadedNotif(_ callback:@escaping ()->Void){
        NotificationCenter.default.addObserver(forName: RFAdmobRewardNotif.loadedNotifName, object: nil, queue: .main) { notif in
            callback()
        }
    }
    
    public static func postRewardLoadeErrNotif(_ err: NSError?){
        NotificationCenter.default.post(name: RFAdmobRewardNotif.loadErrNotifName, object: err)
    }
    public static func addRewardLoadErrNotif(_ callback:@escaping (NSError?)->Void){
        NotificationCenter.default.addObserver(forName: RFAdmobRewardNotif.loadErrNotifName, object: nil, queue: .main) { notif in
            callback(notif.object as? NSError)
        }
    }
    
    public static func postRewardShowedNotif(){
        NotificationCenter.default.post(name: RFAdmobRewardNotif.showedNotifName, object: nil)
    }
    public static func addRewardShowedNotif(_ callback:@escaping ()->Void){
        NotificationCenter.default.addObserver(forName: RFAdmobRewardNotif.showedNotifName, object: nil, queue: .main) { notif in
            callback()
        }
    }
    
    public static func postRewardDismissNotif(){
        NotificationCenter.default.post(name: RFAdmobRewardNotif.dismissNotifName, object: nil)
    }
    public static func addRewardDismissNotif(_ callback:@escaping ()->Void){
        NotificationCenter.default.addObserver(forName: RFAdmobRewardNotif.dismissNotifName, object: nil, queue: .main) { notif in
            callback()
        }
    }
    public static func postRewardWillDismissNotif(){
        NotificationCenter.default.post(name: RFAdmobRewardNotif.willDismissNotifName, object: nil)
    }
    public static func addRewardWillDismissNotif(_ callback:@escaping ()->Void){
        NotificationCenter.default.addObserver(forName: RFAdmobRewardNotif.willDismissNotifName, object: nil, queue: .main) { notif in
            callback()
        }
    }
    
    public static func postRewardShowErrNotif(_ err: NSError?){
        NotificationCenter.default.post(name: RFAdmobRewardNotif.showErrNotifName, object: err)
    }
    public static func addRewardShowErrNotif(_ callback:@escaping (NSError?)->Void){
        NotificationCenter.default.addObserver(forName: RFAdmobRewardNotif.showErrNotifName, object: nil, queue: .main) { notif in
            callback(notif.object as? NSError)
        }
    }
    ///激烈视频观看完毕，激励到手时通知 传入具体激励值
    public static func postRewardCountNotif(_ count: String){
        NotificationCenter.default.post(name: RFAdmobRewardNotif.rewardNotifName, object: count)
    }
    public static func addRewardCountNotif(_ callback:@escaping (String)->Void){
        NotificationCenter.default.addObserver(forName: RFAdmobRewardNotif.rewardNotifName, object: nil, queue: .main) { notif in
            callback(notif.object as? String ?? "")
        }
    }
}
